//
//  BannerWithSoreCollectionViewCell.swift
//  xmly
//
//  Created by 刘文超 on 2019/12/27.
//  Copyright © 2019 刘文超. All rights reserved.
//

import UIKit
import FSPagerView
class BannerWithSoreCollectionViewCell: UICollectionViewCell {
    
    
    let bannerArr:[String] = ["http://n.sinaimg.cn/ent/4_img/upload/cf535fca/20171107/DQ1J-fynmzrs7706384.jpg","http://p2.ifengimg.com/cmpp/2016/05/11/09/a478e0e2-f9c3-4a0b-aae6-f9823142a44c_size236_w500_h277.jpg","http://img.zcool.cn/community/01966e5812b687a84a0d304f41a430.jpg@1280w_1l_2o_100sh.png","http://img.zcool.cn/community/0136835a0935ada80121985c4cf409.jpg@1280w_1l_2o_100sh.jpg","http://www.3dmgame.com/uploads/allimg/160111/153_160111142436_1.jpg","http://img1.gtimg.com/14/1457/145736/14573682_1200x1000_0.jpg","http://www.xxsb.com/pic/2019-11/28/96a63da7-fb07-442b-9ff6-97bc8f80fdce.jpg","http://pic257.nipic.com/file/20191203/24977360_145740057000_2.jpg"]
    let soreArr = [["icon":"大咖主播","name":"关羽"],["icon":"边听变看","name":"张飞"],["icon":"活动","name":"夏侯惇"],["icon":"电子书城","name":"刘备"],["icon":"直播微课","name":"曹操"],["icon":"直播微课","name":"曹操"],["icon":"直播微课","name":"曹操"],["icon":"直播微课","name":"曹操"],["icon":"大咖主播","name":"关羽"],["icon":"边听变看","name":"张飞"]]

    
    
    //懒加载轮播图
    lazy var bannerView:FSPagerView  = {
        let banner = FSPagerView()
        banner.automaticSlidingInterval = 3;
        banner.isInfinite = true
        banner.delegate = self
        banner.dataSource = self
        banner.interitemSpacing = 15
        banner.transformer = FSPagerViewTransformer.init(type: .linear)
        banner.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        banner.backgroundColor = UIColor.white
        return banner
    }()
    //懒加载分类图
    lazy var collectionView:UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout.init()
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.sectionInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0    )
        let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.backgroundColor = UIColor.white
        collectionView.register(RecommendGridCollectionViewCell.self, forCellWithReuseIdentifier: "RecommendGridCollectionViewCell")
        collectionView.register(RecommendNewsCollectionViewCell.self, forCellWithReuseIdentifier: "RecommendNewsCollectionViewCell")
        collectionView.isScrollEnabled = false
        collectionView.dataSource = self
        collectionView.delegate = self
        return collectionView
    }()
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI(){
        //添加banner图
        self.addSubview(self.bannerView)
        self.bannerView.itemSize = CGSize.init(width: ScreenWidth-60, height: 160)
        self.bannerView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(160)
            make.top.equalTo(KBlackspace)
        }
        
       //添加分类试图
        self.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(self.bannerView.snp.bottom).offset(KBlackspace)
            make.bottom.equalToSuperview()
        }
        
    }
    
}
//Mark： - 轮播图代理
extension BannerWithSoreCollectionViewCell:FSPagerViewDelegate,FSPagerViewDataSource{
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.bannerArr.count;
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
         cell.imageView?.kf.setImage(with: URL(string:self.bannerArr[index]))
        cell.backgroundColor = UIColor.white
        return cell
    }
  
}


extension BannerWithSoreCollectionViewCell:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return soreArr.count;
        }
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0 {
             //分类
            let cell:RecommendGridCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecommendGridCollectionViewCell", for: indexPath) as! RecommendGridCollectionViewCell
            cell.dataInfo(dict: soreArr[indexPath.row] as NSDictionary)
            return cell
        }else {
            //通告
            let cell:RecommendNewsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecommendNewsCollectionViewCell", for: indexPath) as! RecommendNewsCollectionViewCell
            cell.backgroundColor = UIColor.white
            return cell
            
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
            return CGSize.init(width: (ScreenWidth-5)/5, height: 80)
        }else{
            return CGSize.init(width: ScreenWidth, height: 50)
        }
    }
    
    
    
}
